package com.ym.manager.admin.t3;

import com.ym.manager.admin.act.model.Act;
import com.ym.manager.admin.t3.model.T3Info;
import com.ym.manager.sys.model.AdminUser;
import com.ym.utils.DateUtil;
import com.ym.utils.Pages;
import com.ym.utils.ResultUtil;
import com.ym.web.bind.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/t3/")
public class T3Controller {

    @Autowired
    private T3Model t3Model;

    /**
     * 页面：活动列表
     * @return
     */
    @RequestMapping("list")
    public String list(){
        return "/t3/list";
    }

    /**
     * 功能：活动列表
     * @return
     */
    @RequestMapping("listData")
    @ResponseBody
    public String listData(@CurrentUser AdminUser user,
                           @RequestParam(defaultValue = "0") int page,
                           @RequestParam(defaultValue = "10") int limit,
                           @RequestParam(defaultValue = "") String title,
                           @RequestParam(defaultValue = "-1") int status){
        Map<String,Object> map = ResultUtil.result();
        try {
            Pages<T3Info> pages = t3Model.listData(page,limit,user.getMerchant_id(),title,status);
            //查询数据
            map.put("data",pages.getList());
            map.put("count",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",-1000);
            map.put("msg","系统异常");
        }
        return ResultUtil.toJSON(map);
    }

    /**
     * 页面：新增活动
     * @return
     */
    @GetMapping("add")
    public String add(){
        return "/t3/add";
    }

    /**
     * 功能：新增活动
     * @param t3Info
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public String add(T3Info t3Info){
        Map<String,Object> result = ResultUtil.result();
        try {
            int i;
            if(t3Info.getId()>0){
                i = t3Model.update(t3Info);
                result.put("code",i>0?0:-1);
                result.put("msg",i>0?"修改成功":"修改失败");
            }else{
                i = t3Model.insert(t3Info);
                result.put("code",i>0?0:-1);
                result.put("msg",i>0?"新增成功":"新增失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 页面：新增活动
     * @return
     */
    @GetMapping("edit")
    public String edit(Model model, int id){
        model.addAttribute("t3",t3Model.detail(id));
        return "/t3/edit";
    }

    /**
     * 功能：活动上下架
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("shelf")
    @ResponseBody
    public String shelf(@RequestParam(defaultValue = "0") int id,
                        @RequestParam(defaultValue = "0")int status){
        Map<String,Object> result = ResultUtil.result();
        if(id<=0 || status <=0){
            result.put("code",-1000);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            int i = t3Model.shelf(id,status);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?status==1?"下线成功":"上线成功":i==0?"下线失败，请联系管理员":"上线失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：删除活动
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public String delete(@RequestParam(defaultValue = "0") int id){
        Map<String,Object> result = ResultUtil.result();
        if(id<=0){
            result.put("code",-1000);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            int i = t3Model.delete(id);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?"删除成功":"删除失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    @RequestMapping("detail/subscribe")
    public String subscribeDetail(Model model,int id){
        model.addAttribute("id",id);
        return "/t3/subscribe";
    }

    @RequestMapping("detail/subscribeData")
    @ResponseBody
    public String subscribeDetail(@RequestParam(defaultValue = "0") int page,
                                  @RequestParam(defaultValue = "10") int limit,
                                  int id){
        Map<String,Object> map = ResultUtil.result();
        try {
            Pages<Map<String,Object>> pages = t3Model.subscribeDetail(page,limit,id);
            //查询数据
            map.put("data",pages.getList());
            map.put("count",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",-1000);
            map.put("msg","系统异常");
        }
        return ResultUtil.toJSON(map);
    }

    @RequestMapping("subscribe/arrival")
    @ResponseBody
    public String subscribeArrival(@RequestParam(defaultValue = "0")int uid,
                                   @RequestParam(defaultValue = "0")int actId,
                                   @RequestParam(defaultValue = "1")int status){
        Map<String,Object> result = ResultUtil.result();
        if(uid<=0 || actId <= 0){
            result.put("code",-1000);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            int i = t3Model.subscribeArrival(uid,actId,status);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?"核销成功":"核销失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }
}
